var ModelVehicleObject="com.venta.autopartes.car.ModelVehicle";
var stateObject="com.venta.autopartes.car.State";
var townObject="com.venta.autopartes.car.Town";
var suburbObject="com.venta.autopartes.car.Suburb";

function json_vehicle(jsonUrl,elementIdAffected,element)
{	
	if(element.selectedIndex!=0){
		var paramValue=element.id+"="+element.options[element.selectedIndex].value;
		$.ajax({
			type: "POST",
			url: jsonUrl+"?"+paramValue,
			dataType: "json",
			data: "{}",
			contentType: "application/json; charset=utf-8",
			success: function(data) {							              
				var selectElement=document.getElementById(elementIdAffected);
				var jsonObject=data.list[0];				
				
				deleteOptions(selectElement);
				if (jsonObject[ModelVehicleObject]!=null){
					createModelVehicleElement(jsonObject,selectElement);
				}
					
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
							alert(textStatus);
				}
			});
	}
}

function createModelVehicleElement(jsonObject,selectElement){
	if (jsonObject[ModelVehicleObject].modelVehicle!=null){
		var elOptNew = document.createElement('option');
	    elOptNew.text = jsonObject[ModelVehicleObject].modelVehicle;
	    elOptNew.value =  jsonObject[ModelVehicleObject].modelVehicleId; 
	    try {
	    	selectElement.add(elOptNew); // standards compliant; doesn't work in IE
	    }
	    catch(ex) {
	    	selectElement.add(elOptNew, selectElement.selectedIndex); // IE only
	    }
	}else{
		for(var i=0;i<jsonObject[ModelVehicleObject].length;i++){
			var elOptNew = document.createElement('option');
		    elOptNew.text =  jsonObject[ModelVehicleObject][i].modelVehicle;
		    elOptNew.value =  jsonObject[ModelVehicleObject][i].modelVehicleId; 
		    try {
		    	selectElement.add(elOptNew); // standards compliant; doesn't work in IE
		    }
		    catch(ex) {
		    	selectElement.add(elOptNew, selectElement.selectedIndex); // IE only
		    }
		}
	}
}

function deleteOptions(selectElement){
	for (var i = selectElement.length-1; i>0; i--) {
		selectElement.remove(i);
	}
}

function json_location(jsonUrl,elementIdAffected,element)
{	
	if(element.selectedIndex!=0){
		var paramValue=element.id+"="+element.options[element.selectedIndex].value;
		$.ajax({
			type: "POST",
			url: jsonUrl+"?"+paramValue,
			dataType: "json",
			data: "{}",
			contentType: "application/json; charset=utf-8",
			success: function(data) {							              
				var selectElement=document.getElementById(elementIdAffected);
				var jsonObject=null;		
				
				if(elementIdAffected=="stateId"){
					jsonObject=data.locationList.states[0];	
					deleteOptions(selectElement);
					if (jsonObject[stateObject]!=null){
						createStateElement(jsonObject,selectElement);
					}
				}else if(elementIdAffected=="townId"){
					jsonObject=data.locationList.towns[0];
					deleteOptions(selectElement);
					if (jsonObject[townObject]!=null){
						createTownElement(jsonObject,selectElement);
					}
				}else if(elementIdAffected=="suburbId"){
					jsonObject=data.locationList.suburbs[0];
					deleteOptions(selectElement);
					if (jsonObject[suburbObject]!=null){
						createSuburbElement(jsonObject,selectElement);
					}
				}else if(elementIdAffected=="zipcode"){
					jsonObject=data.locationList.zipcode;
					//deleteOptions(selectElement);
					if (jsonObject!=null){
				    	var zipcodeInputbox=document.getElementById("zipcode");
				    	zipcodeInputbox.value=jsonObject.zipcode;
						//createSuburbElement(jsonObject,selectElement);
					}
				}
				
				
				
					
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
							alert(textStatus);
				}
			});
	}
}

function createStateElement(jsonObject,selectElement){
	if (jsonObject[stateObject].stateId!=null){
		var elOptNew = document.createElement('option');
	    elOptNew.text = jsonObject[stateObject].state;
	    elOptNew.value =  jsonObject[stateObject].stateId; 
	    try {
	    	selectElement.add(elOptNew); // standards compliant; doesn't work in IE
	    }
	    catch(ex) {
	    	selectElement.add(elOptNew, selectElement.selectedIndex); // IE only
	    }
	}else{
		for(var i=0;i<jsonObject[stateObject].length;i++){
			var elOptNew = document.createElement('option');
		    elOptNew.text =  jsonObject[stateObject][i].state;
		    elOptNew.value =  jsonObject[stateObject][i].stateId; 
		    try {
		    	selectElement.add(elOptNew); // standards compliant; doesn't work in IE
		    }
		    catch(ex) {
		    	selectElement.add(elOptNew, selectElement.selectedIndex); // IE only
		    }
		}
	}
}

function createTownElement(jsonObject,selectElement){
	if (jsonObject[townObject].townId!=null){
		var elOptNew = document.createElement('option');
	    elOptNew.text = jsonObject[townObject].town;
	    elOptNew.value =  jsonObject[townObject].townId; 
	    try {
	    	selectElement.add(elOptNew); // standards compliant; doesn't work in IE
	    }
	    catch(ex) {
	    	selectElement.add(elOptNew, selectElement.selectedIndex); // IE only
	    }
	}else{
		for(var i=0;i<jsonObject[townObject].length;i++){
			var elOptNew = document.createElement('option');
		    elOptNew.text =  jsonObject[townObject][i].town;
		    elOptNew.value =  jsonObject[townObject][i].townId; 
		    try {
		    	selectElement.add(elOptNew); // standards compliant; doesn't work in IE
		    }
		    catch(ex) {
		    	selectElement.add(elOptNew, selectElement.selectedIndex); // IE only
		    }
		}
	}
}

function createSuburbElement(jsonObject,selectElement){
	if (jsonObject[suburbObject].suburbId!=null){
		var elOptNew = document.createElement('option');
	    elOptNew.text = jsonObject[suburbObject].suburb;
	    elOptNew.value =  jsonObject[suburbObject].suburbId; 
	    try {
	    	selectElement.add(elOptNew); // standards compliant; doesn't work in IE
	    }
	    catch(ex) {
	    	selectElement.add(elOptNew, selectElement.selectedIndex); // IE only
	    }
	}else{
		for(var i=0;i<jsonObject[suburbObject].length;i++){
			var elOptNew = document.createElement('option');
		    elOptNew.text =  jsonObject[suburbObject][i].suburb;
		    elOptNew.value =  jsonObject[suburbObject][i].suburbId; 
		    try {
		    	selectElement.add(elOptNew); // standards compliant; doesn't work in IE
		    }
		    catch(ex) {
		    	selectElement.add(elOptNew, selectElement.selectedIndex); // IE only
		    }
		}
	}
}
